package domain.advertisement;

import java.io.Serializable;

import javax.swing.ImageIcon;

import application.config.Config;
import application.config.Config.AdvertiseStatus;
import domain.users.User;
/* Ad class is reposible for storing the advertisement. That inludes
 * an image, description. The status is for the admin to approve the advertisement. 
 */
public class Ad implements Serializable {

	//private static final long serialVersionUID = 6622782781189290194L;
	private ImageIcon image;
	private String description;
	private AdvertiseStatus status;
	private AdvertisementScheme scheme;
	private User advertiser;
	
	private int clicks;
	private int views;

	public Ad(User advertiser, ImageIcon image,
			String description, AdvertisementScheme scheme, AdvertiseStatus status) {
		this.advertiser = advertiser;
		this.image = image;
		this.description = description;
		this.scheme = scheme;
		this.status = status;
	}

	/** Returns the image for the Ad object */
	public ImageIcon getImage() {
		return image;
	}

	/** Returns the description for the Ad object */
	public String getDescription() {
		return description;
	}
	
	/** Sets the description for the Ad object */
	public void setDescription(String description) {
		this.description = description;
	}

	/** Returns the status for the Ad object */
	public AdvertiseStatus getStatus() {
		return status;
	}

	/** Sets the status for the Ad object */
	public void setStatus(AdvertiseStatus status) {
		this.status = status;
	}

	/** Returns the AdvertisementScheme for the Ad object */
	public AdvertisementScheme getScheme() {
		return scheme;
	}

	/** Returns the User (advertiser) object for the Ad object */
	public User getAdvertiser() {
		return advertiser;
	}

	/** Returns the number of clicks for the ad */
	public int getClicks() {
		return clicks;
	}
	
	/** Returns the number of views for the ad */
	public int getViews() {
		return views;
	}

	/*
	 * This method increases the number of clicks.
	 * If the number of clicks is more than the scheme
	 * the advertisement status becomes closed.
	 * @param clicks
	 */
	public void incrClicks(int clicks) {
		if (clicks >= scheme.getClicks()) 
			status = Config.AdvertiseStatus.CLOSED;
		else
			this.clicks += clicks;
	}
	
	/*
	 * This method increases the number of views.
	 * If the number of views is more than the scheme
	 * the advertisement status becomes closed.
	 * @param views
	 */
	public void incrViews(int views) {
		if (clicks >= scheme.getClicks())
			status = Config.AdvertiseStatus.CLOSED;
		else
			this.views += views;
	}

	@Override
	public String toString() {
		return ("Ad [ advertiser = " + advertiser.getUserId() + ", image = " + image
				+ "\n, " + scheme + " ]");
	}	

}
